package com.reliable.service.serviceimpl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reliable.api.param.ReliablePushTemplate.ReliablePushDetailPageParam;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.utils.uuid.IdUtils;
import com.reliable.service.domain.ReliableExportPushRecord;
import com.reliable.service.mapper.ReliableExportPushRecordMapper;
import com.reliable.service.utils.IdEncryptUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reliable.service.mapper.ReliablePushTemplateMapper;
import com.reliable.service.domain.ReliablePushTemplate;
import com.reliable.service.service.IReliablePushTemplateService;
import com.reliable.api.dto.ReliableExportPushRecord.ReliablePushTemplateStatDTO;
import com.reliable.api.param.ReliablePushTemplate.ReliablePushTemplatePageParam;
import com.reliable.api.dto.ReliableExportPushRecord.ReliableExportPushRecordDetailDTO;

/**
 * 推送目标模板Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-29
 */
@Service
public class ReliablePushTemplateServiceImpl extends ServiceImpl<ReliablePushTemplateMapper, ReliablePushTemplate> implements IReliablePushTemplateService {

    @Autowired
    private ReliablePushTemplateMapper reliablePushTemplateMapper;

    @Autowired
    private ReliableExportPushRecordMapper reliableExportPushRecordMapper;

    /**
     * 查询推送目标模板
     *
     * @param id 推送目标模板主键
     * @return 推送目标模板
     */
    @Override
    public ReliablePushTemplate selectReliablePushTemplateById(Long id) {
        return reliablePushTemplateMapper.selectById(id);
    }

    /**
     * 查询推送目标模板列表
     *
     * @param reliablePushTemplate 推送目标模板
     * @return 推送目标模板
     */
    @Override
    public List<ReliablePushTemplate> selectReliablePushTemplateList(ReliablePushTemplate reliablePushTemplate) {
        return reliablePushTemplateMapper.selectReliablePushTemplateList(reliablePushTemplate);
    }

    /**
     * 新增推送目标模板
     *
     * @param reliablePushTemplate 推送目标模板
     * @return 结果
     */
    @Override
    public int insertReliablePushTemplate(ReliablePushTemplate reliablePushTemplate) {
        return reliablePushTemplateMapper.insert(reliablePushTemplate);
    }

    /**
     * 修改推送目标模板
     *
     * @param reliablePushTemplate 推送目标模板
     * @return 结果
     */
    @Override
    public int updateReliablePushTemplate(ReliablePushTemplate reliablePushTemplate) {
        return reliablePushTemplateMapper.updateById(reliablePushTemplate);
    }

    /**
     * 批量删除推送目标模板
     *
     * @param ids 需要删除的推送目标模板主键
     * @return 结果
     */
    @Override
    public int deleteReliablePushTemplateByIds(Long[] ids) {
        return reliablePushTemplateMapper.deleteBatchIds(java.util.Arrays.asList(ids));
    }

    /**
     * 删除推送目标模板信息
     *
     * @param id 推送目标模板主键
     * @return 结果
     */
    @Override
    public int deleteReliablePushTemplateById(Long id) {
        return reliablePushTemplateMapper.deleteById(id);
    }
    
    /**
     * 查询推送模板统计列表(带分页)
     *
     * @param param 查询参数和分页信息
     * @return 推送模板统计列表
     */
    @Override
    public List<ReliablePushTemplateStatDTO> selectReliablePushTemplateStatList(ReliablePushTemplatePageParam param) {
        return reliablePushTemplateMapper.selectReliablePushTemplateStatList(param);
    }

    @Override
    public AjaxResult pushDetailList(ReliablePushDetailPageParam param) {

        Long expoid = IdEncryptUtils.decrypt(param.getExportBusinessId());

        Page<ReliableExportPushRecord> page = new Page<>(param.getPageNum(), param.getPageSize());

        LambdaQueryWrapper<ReliableExportPushRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReliableExportPushRecord::getExportRecordId, expoid);

        if (param.getTemplateStatus() != null) {
            queryWrapper.eq(ReliableExportPushRecord::getPushStatus, param.getTemplateStatus());
        }

        Date pushTimeStart = param.getPushTimeStart();
        if (pushTimeStart != null) {
            queryWrapper.ge(ReliableExportPushRecord::getPushTime, pushTimeStart);
        }
        Date pushTimeEnd = param.getPushTimeEnd();
        if (pushTimeEnd != null)
        {
            queryWrapper.le(ReliableExportPushRecord::getPushTime, pushTimeEnd);
        }
        queryWrapper.orderByDesc(ReliableExportPushRecord::getPushTime);

        Page<ReliableExportPushRecord> recordPage = reliableExportPushRecordMapper.selectPage(page, queryWrapper);
        
        // 将查询结果转换为DTO
        List<ReliableExportPushRecordDetailDTO> records = recordPage.getRecords().stream().map(record -> {
            ReliableExportPushRecordDetailDTO dto = new ReliableExportPushRecordDetailDTO();
            dto.setId(record.getId());
            dto.setChannel(record.getChannel());
            dto.setCity(record.getCity());
            dto.setDepartment(record.getDepartment());
            dto.setPushStatus(record.getPushStatus());
            dto.setPushTime(record.getPushTime());
            dto.setRecordCount(record.getRecordCount());
            dto.setFilePath(record.getFilePath());
            dto.setErrorMessage(record.getErrorMessage());
            dto.setCreateTime(record.getCreateTime());
            return dto;
        }).collect(Collectors.toList());

        Page<ReliableExportPushRecordDetailDTO> dtoPage = new Page<>(recordPage.getCurrent(), recordPage.getSize(), recordPage.getTotal());
        dtoPage.setRecords(records);
        
        return AjaxResult.success(dtoPage);
    }
}